/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.UserGroupDAO;
import exatrez.manager.UserObsManager;
import exatrez.model.InfoAudit;
import exatrez.model.User;
import exatrez.model.UserGroup;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.ObservationConfiguration;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */

@UrlBinding("/adminObsStatus")
@AuthRequired
public class UserObsAdminAction extends BaseActionBean{
    
    private static Logger LOG = LoggerFactory.getLogger(UserObsAdminAction.class);
    
    @Inject
    ObservationConfiguration obsConf;
    @Inject
    private UserGroupDAO userGroupDAO;
    @Inject
    private UserObsManager ubManager;
    
    private static final String VIEWUSER = "admin/user_obs_view.jsp";
    private static final String EDITUSER = "admin/user_obs_edit.jsp";
    
    private List<UserGroup> listUser;
    private UserGroup userDetail;
    
    private String groupId;
    
    @DefaultHandler
    public Resolution viewUser(){
        LOG.debug("method viewUser start");
        listUser = ubManager.listSelectedGroup();
        LOG.debug("listUser.size :"+listUser.size());
        LOG.debug("method viewUser finish");
        return new JSP(VIEWUSER);
    }
    
    public Resolution editUser(){
        LOG.debug("method editUser start");
        groupId = getContext().getRequest().getParameter("id");
        if(groupId.isEmpty()){
            addSimpleError("Id User is null. Please provide User ID.");
            return new RedirectResolution(UserObsAdminAction.class, "viewUser");
        }
        try{
            userDetail = userGroupDAO.findById(Integer.parseInt(groupId));
            LOG.debug("userID :"+userDetail.getUser().getUserId()+", Group :"+userDetail.getGroup().getGroupName());
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only\
            LOG.error("FAIL : "+ex.getMessage());
            addSimpleError("FAIL : Please see Administrator for more information.");
        }
        
        LOG.debug("method editUser finish");
        return new JSP(EDITUSER).addParameter("popup", true);
    }
    
    public Resolution save(){
        LOG.debug("method save start");
        User user = getContext().getCurrentUser();
        try{
            LOG.debug("groupId :"+groupId);
            LOG.debug("userDetail.getObsFlag() :"+userDetail.getObsFlag());
            UserGroup ug = userGroupDAO.findById(Integer.parseInt(groupId));
            ug.setObsFlag(userDetail.getObsFlag());
            InfoAudit ia = ug.getInfoAudit();
            ia.setUpdateId(user);
            ia.setUpdateDate(new Date());
            ug.setInfoAudit(ia);
            LOG.debug("ug.getObsFlag( :"+ug.getObsFlag());
            String result = ubManager.saveUserGroup(ug);
            if(result.equals("success")){
                LOG.debug("success Save");
                addSimpleMessage("Update Success.");
            }else{
                LOG.debug("fail Save");
                addSimpleError("Fail to update.");
            }
        }catch(Exception ex){
            ex.printStackTrace(); // for development used only
            LOG.debug("FAIL : "+ex.getMessage());
            addSimpleError("FAIL : Information not complete. Please see Administrator for more information");
        }
        LOG.debug("method save finish");
        return new RedirectResolution(UserObsAdminAction.class, "viewUser");
    }

    public List<UserGroup> getListUser() {
        return listUser;
    }

    public void setListUser(List<UserGroup> listUser) {
        this.listUser = listUser;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public UserGroup getUserDetail() {
        return userDetail;
    }

    public void setUserDetail(UserGroup userDetail) {
        this.userDetail = userDetail;
    }
    
}
